/**
 * 演示程序当前的 “注册/登录” 等操作，是基于 “本地存储” 完成的
 * 当您要参考这个演示程序进行相关 app 的开发时，
 * 请注意将相关方法调整成 “基于服务端Service” 的实现。
 **/
(function($, owner) {
	/**
	 * 用户登录
	 **/
	owner.login = function(loginInfo, callback) {
		callback = callback || $.noop;
		loginInfo = loginInfo || {};
		loginInfo.account = loginInfo.account || '';
		loginInfo.password = loginInfo.password || '';
		var url = baseUrl + "/dapi/m_user/login/" + loginInfo.account + "/" + loginInfo.password;
		mui.ajax(url, {
			// data:regInfo,
			dataType: 'json', //服务器返回json格式数据
			type: 'get', //HTTP请求类型	
			timeout: 10000, //超时时间设置为10秒；
			headers: {
				'Content-Type': 'application/json'
			},
			success: function(data) {
				var daStatus = data.status;
				if(daStatus == '1') {
					var userInfo = {
						id: data.data.id,
						userName: data.data.userName,
						password: loginInfo.password,
						phone: data.data.phone,
						realName: data.data.realName,
						registCode: data.data.registCode,
						checkStatus: data.data.checkStatus,
						ifLogin: '1'
					};
					var users = JSON.parse(localStorage.getItem('$users') || '[]');
					users.push(userInfo);
					localStorage.setItem('$users', JSON.stringify(users));
					return owner.createState(loginInfo.account, callback);
				} else {
					return callback(data.message);
				}

			},
			error: function(data) {
				return callback('登录失败');
			}
		});
	};

	owner.createState = function(name, callback) {
		var state = owner.getState();
		state.account = name;
		state.token = "token" + state.account;
		owner.setState(state);
		return callback();
	};

	/**
	 * 新用户注册
	 **/
	owner.reg = function(regInfo, callback) {
		callback = callback || $.noop;
		regInfo = regInfo || {};
		regInfo.userName = regInfo.userName || '';
		regInfo.password = regInfo.password || '';
		//var baseUrl="http://192.168.1.165:8777";
		var url = baseUrl + "/dapi/muser/register";
		$.ajax(url, {
			data: regInfo,
			dataType: 'json', //服务器返回json格式数据
			type: 'post', //HTTP请求类型	
			timeout: 10000, //超时时间设置为10秒；
			contentType: 'application/json',
			success: function(data) {
				if(data.status==1){
					regInfo.registCode = data.data;
					regInfo.checkStatus = "0";
					var users = JSON.parse(localStorage.getItem('$users') || '[]');
					users.push(regInfo);
					localStorage.setItem('$users', JSON.stringify(users));
					$.openWindow({
						url: 'verify_reg.html',
						id: 'verify_reg',
						show: {
							aniShow: 'pop-in'
						},
						extras: {
							regNum: data.data
						}
					});
					return callback('注册成功，您的注册码为' + data.data);
				}else if(data.status==0){
					return callback('注册失败，'+data.message);
				}
			},error: function() {
				return callback('注册失败，请重新注册');
			}
		});

		return callback();
	};
	/**
	 *用户注册信息修改
	 **/
	owner.updateReg = function(regInfo, callback) {
		callback = callback || $.noop;
		regInfo = regInfo || {};
		regInfo.userName = regInfo.userName || '';
		regInfo.password = regInfo.password || '';
		//var baseUrl="http://192.168.1.165:8777";
		var url = baseUrl + "/dapi/muser/registerUpdate";
		$.ajax(url, {
			data: regInfo,
			dataType: 'json', //服务器返回json格式数据
			type: 'post', //HTTP请求类型	
			timeout: 10000, //超时时间设置为10秒；
			headers: {
				'Content-Type': 'application/json'
			},
			success: function(data) {
				regInfo.checkStatus = "0";
				regInfo.registCode = data.data;
				var users = JSON.parse(localStorage.getItem('$users') || '[]');
				users.push(regInfo);
				localStorage.setItem('$users', JSON.stringify(users));
				$.openWindow({
					url: 'verify_reg.html',
					id: 'verify_reg',
					show: {
						aniShow: 'pop-in'
					}
				});
				return callback('信息修改成功');
			},
			error: function(data) {
				return callback('注册信息修改失败');
			}
		});

		return callback();
	};
	/**
	 * 获取当前状态
	 **/
	owner.getState = function() {
		var stateText = localStorage.getItem('$state') || "{}";
		return JSON.parse(stateText);
	};

	/**
	 * 设置当前状态
	 **/
	owner.setState = function(state) {
		state = state || {};
		localStorage.setItem('$state', JSON.stringify(state));
		//var settings = owner.getSettings();
		//settings.gestures = '';
		//owner.setSettings(settings);
	};

	/**
	 * 找回密码
	 **/
	owner.forgetPassword = function(email, callback) {
		callback = callback || $.noop;
		if(!checkEmail(email)) {
			return callback('邮箱地址不合法');
		}
		return callback(null, '新的随机密码已经发送到您的邮箱，请查收邮件。');
	};

	/**
	 * 获取应用本地配置
	 **/
	owner.setSettings = function(settings) {
		settings = settings || {};
		localStorage.setItem('$settings', JSON.stringify(settings));
	}

	/**
	 * 设置应用本地配置
	 **/
	owner.getSettings = function() {
		var settingsText = localStorage.getItem('$settings') || "{}";
		return JSON.parse(settingsText);
	}
	/**
	 * 获取本地是否安装客户端
	 **/
	owner.isInstalled = function(id) {
		if(id === 'qihoo' && mui.os.plus) {
			return true;
		}
		if(mui.os.android) {
			var main = plus.android.runtimeMainActivity();
			var packageManager = main.getPackageManager();
			var PackageManager = plus.android.importClass(packageManager)
			var packageName = {
				"qq": "com.tencent.mobileqq",
				"weixin": "com.tencent.mm",
				"sinaweibo": "com.sina.weibo"
			}
			try {
				return packageManager.getPackageInfo(packageName[id], PackageManager.GET_ACTIVITIES);
			} catch(e) {}
		} else {
			switch(id) {
				case "qq":
					var TencentOAuth = plus.ios.import("TencentOAuth");
					return TencentOAuth.iphoneQQInstalled();
				case "weixin":
					var WXApi = plus.ios.import("WXApi");
					return WXApi.isWXAppInstalled()
				case "sinaweibo":
					var SinaAPI = plus.ios.import("WeiboSDK");
					return SinaAPI.isWeiboAppInstalled()
				default:
					break;
			}
		}
	}
}(mui, window.app = {}));